﻿using OpenCvSharp.XImgProc;
using OpenCvSharp;
using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.constant;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using SUNRPA.CONTROLS.IMPL.domain.flowRunningVar;
using SUNRPA.CONTROLS.IMPL.util;
using System.Collections;
using System.Collections.Concurrent;

namespace SUNRPA.CONTROLS.IMPL.controls {
    /// <summary>
    /// TODO: 1、等待元素出现
    ///       2、匹配模式中的正则表达式匹配方式
    /// </summary>
    public class SetWebDDLNAction : BaseControlActions {
        public override int Execute(int currentIndex, FlowItem item, ConcurrentDictionary<string, ConcurrentDictionary<string, RPAVariableDefineWrapper>> globleData, List<FlowItem> flowItems) {
            SetWebDDLNParam? param = item.ControlParam as SetWebDDLNParam;
            WebElement ele = param.WebEle as WebElement;
            //判断是否为web元素
            if(ele == null){ 
                throw new RpaRuntimeException("元素不是web元素，无法进行web输入操作");
            }
            string webObject = param.WebObj;
            WebPage webPage = this.GetObjVar<WebPage>(ref webObject, globleData);
            if(webPage == null){ 
                throw new RpaRuntimeException("网页为空，无法进行web输入操作");
            }
            string delay = param.DelayTime;
            this.GetStringVar(ref delay, globleData);
            int delayTime = 0;
            if(!string.IsNullOrEmpty(delay) && (!int.TryParse(delay, out delayTime) || delayTime < 0)) { 
                throw new RpaRuntimeException("输入后延迟时间必须是大于等于零的数字或者为空");
            }

            string selectValue = param.SelectValue;
            this.GetStringVar(ref selectValue, globleData);

            int selectType = param.SelectType;
            int matchMethod = param.MatchMethod;

            switch(webPage.Bt){ 
                case BrowserType.GOOGLE:
                    using(NamedPipeClientUtil client = new NamedPipeClientUtil("localhost"
                        , DefaultContents.NAMED_PIPE_SERVER, System.IO.Pipes.PipeDirection.InOut)) {
                    NamePipeRequest request = new NamePipeRequest();
                        if(selectType == 0) {
                            request.method = DefaultContents.SELECT_DDV;
                        } else { 
                            request.method = DefaultContents.SELECT_DDV_BYINDEX;
                            if(!int.TryParse(selectValue, out int selectIndex) || selectIndex < 0) {
                                throw new RpaRuntimeException("按照位置选择，选择值必须为大于等于0整数");
                            }
                        }
                        request.requestData = new Dictionary<string, object>() { { "tabId", webPage.TabId}, 
                            { "ele", ele }, { "matchMethod", matchMethod },{ "selectValue", selectValue} };
                        BridgeResponse resp = client.SendRequest(request);
                        if(!resp.code.Equals(CommunicationCode.OK)) {
                            throw new RpaRuntimeException($"设置下拉框选项出错:{resp.msg}");
                        }
                    }
                    break;
                case BrowserType.IE:
                    //TODO
                    break;
                case BrowserType.EDGE:
                    //TODO
                    break;
                default:
                    throw new RpaRuntimeException("浏览器类型错误");
            }
            Thread.Sleep(delayTime * 1000);
            return currentIndex;
        }
    }
}
